--based on the fibonacci sequence
local mobHeights={
	-23300, -14400, -8900, -5500,
	-3400, -2100, -1300, -800,
	-500, -300, -200, -100,
	0
}

local function modify(ent,ix,ra)
	if ra==1 then
		ent.damage=ent.damage+0.4*ix
		ent.hp_min=ent.hp_min+2*ix
		ent.hp_max=ent.hp_max+2*ix
		ent.armor=ent.armor-3*ix
	else
		ent.damage=ent.damage+0.2*ix
		ent.hp_min=ent.hp_min+4*ix
		ent.hp_max=ent.hp_max+4*ix
		ent.armor=ent.armor-2*ix
	end
end

local function advance(obj,textureFu)
	local height=(obj:get_pos()).y
	local ra=Random:next(1,2)
	if height>mobHeights[13] then
		obj:set_properties({textures=textureFu(0,ra)})
	elseif height>mobHeights[12] then
		modify(obj:get_luaentity(),1,ra)
		obj:set_properties({textures=textureFu(-1,ra)})
	elseif height>mobHeights[11] then
		modify(obj:get_luaentity(),2,ra)
		obj:set_properties({textures=textureFu(-2,ra)})
	elseif height>mobHeights[10] then
		modify(obj:get_luaentity(),3,ra)
		obj:set_properties({textures=textureFu(-3,ra)})
	elseif height>mobHeights[9] then
		modify(obj:get_luaentity(),4,ra)
		obj:set_properties({textures=textureFu(-4,ra)})
	elseif height>mobHeights[8] then
		modify(obj:get_luaentity(),5,ra)
		obj:set_properties({textures=textureFu(-5,ra)})
	elseif height>mobHeights[7] then
		modify(obj:get_luaentity(),6,ra)
		obj:set_properties({textures=textureFu(-6,ra)})
	elseif height>mobHeights[6] then
		modify(obj:get_luaentity(),7,ra)
		obj:set_properties({textures=textureFu(-7,ra)})
	elseif height>mobHeights[5] then
		modify(obj:get_luaentity(),8,ra)
		obj:set_properties({textures=textureFu(-8,ra)})
	elseif height>mobHeights[4] then
		modify(obj:get_luaentity(),9,ra)
		obj:set_properties({textures=textureFu(-9,ra)})
	elseif height>mobHeights[3] then
		modify(obj:get_luaentity(),10,ra)
		obj:set_properties({textures=textureFu(-10,ra)})
	elseif height>mobHeights[2] then
		modify(obj:get_luaentity(),11,ra)
		obj:set_properties({textures=textureFu(-11,ra)})
	elseif height>mobHeights[1] then
		modify(obj:get_luaentity(),12,ra)
		obj:set_properties({textures=textureFu(-12,ra)})
	end
end

-------------------------------------------------------------------------------
--Cubo
local function cuboTextures(num,ra)
	if num==0 then
		return {"mobs_dungeon_cubo.png^mobs_dungeon_cubo_overlay0.png"}
	else
		return {"mobs_dungeon_cubo.png^mobs_dungeon_cubo_overlay"
			..num.."_"..ra..".png"
		}
	end
end

mobs:register_mob(
	"mobs_dungeon:cubo", 
	{
		type="monster",
		passive=false,
		damage=3,
		attack_type="shoot",
		shoot_interval=0.5,
		arrow="mobs_dungeon:cubo_arrow",
		shoot_offset=2,
		hp_min=10,
		hp_max=25,
		armor=100,
		collisionbox={-0.5, -1.5, -0.5, 0.5, 0.5, 0.5},
		visual="mesh",
		mesh="zmobs_mese_monster.x",
		textures={
			{"mobs_dungeon_cubo.png^mobs_dungeon_cubo_overlay0.png"},
		},
		makes_footstep_sound=false,
		sounds={
			random="mobs_dungeon_cubo_random",
			attack="mobs_dungeon_cubo_attack",
			shoot_attack="mobs_dungeon_cubo_attack",
			death="mobs_dungeon_cubo_death",
			damage="mobs_dungeon_cubo_damage",
		},
		view_range=10,
		walk_velocity=0.5,
		run_velocity=2,
		jump=true,
		jump_height=8,
		fall_damage=0,
		fall_speed=-6,
		stepheight=2.1,
		drops={},
		water_damage=1,
		lava_damage=1,
		light_damage=0,
		animation={
			speed_normal=15,
			speed_run=15,
			stand_start=0,
			stand_end=14,
			walk_start=15,
			walk_end=38,
			run_start=40,
			run_end=63,
			punch_start=40,
			punch_end=63,
		},
		on_spawn=function(self) advance(self.object,cuboTextures) end
	}	
)

mobs:register_arrow(
	"mobs_dungeon:cubo_arrow", 
	{
		visual="sprite",
		visual_size={x=0.5, y=0.5},
		textures={"mobs_dungeon_cubo_arrow.png"},
		velocity=6,
		hit_player=function(self, player)
			player:punch(
				self.object, 1.0, 
				{full_punch_interval=1.0, damage_groups={fleshy=2}}, nil
			)
		end,
		hit_mob=function(self, player)
			player:punch(
				self.object, 1.0, 
				{full_punch_interval=1.0,damage_groups={fleshy=2}}, nil
			)
		end,
		hit_node=function(self, pos, node)
		end
	}
)

mobs:spawn(
	{
		name="mobs_dungeon:cubo",
		nodes={"group:cracky"},
		chance=3000,
		active_object_count=2,
	}
)

mobs:register_egg("mobs_dungeon:cubo", "Cubo", "mobs_dungeon_cubo_egg.png", 1)

-------------------------------------------------------------------------------
--Animated Block
local function createAnimatedBlockTextureFu(texture)
	local function animatedBlockTextures(num,ra)
		local out={
			texture,
			texture,
			texture,
			texture,
			texture,
			texture,
		}
		if num==0 then
			out[5]=out[5].."^mobs_dungeon_animated_block0.png"
		else
			out[5]=out[5].."^mobs_dungeon_animated_block"
			..num.."_"..ra..".png"
		end
		return out
	end
	return animatedBlockTextures
end

mobs:register_mob(
	"mobs_dungeon:animated_block", 
	{
		type="monster",
		passive=false,
		attack_type="dogfight",
		pathfinding=true,
		reach=2,
		damage=4,
		hp_min=40,
		hp_max=50,
		armor=40,
		collisionbox={-0.5, -0.5, -0.5, 0.5, 0.45, 0.5},
		visual_size={x=1,y=1,z=1},
		visual="cube",
		textures={
			"mobs_dungeon_animated_block0.png",
			"mobs_dungeon_animated_block0.png",
			"mobs_dungeon_animated_block0.png",
			"mobs_dungeon_animated_block0.png",
			"mobs_dungeon_animated_block0.png",
			"mobs_dungeon_animated_block0.png",
		},
		makes_footstep_sound=true,
		sounds={
			random="mobs_dungeon_animated_block_random",
			attack="mobs_dungeon_animated_block_attack",
			death="mobs_dungeon_animated_block_death"
		},
		walk_velocity=0.5,
		run_velocity=1,
		jump=true,
		jump_height=1,
		floats=1,
		view_range=10,
		drops={},
		water_damage=0,
		lava_damage=0,
		light_damage=0,
		on_spawn=function(self)
			local obj=self.object
			local tmp=minetest.find_node_near(obj:get_pos(),2,{"group:cracky"})
			if tmp then
				tmp=string.gsub(minetest.get_node(tmp).name,":","_")..".png"
				advance(obj,createAnimatedBlockTextureFu(tmp))
			end
		end
	}
)

mobs:spawn(
	{
		name="mobs_dungeon:animated_block",
		nodes={"group:cracky"},
		chance=3000,
		active_object_count=2,
	}
)

mobs:register_egg(
	"mobs_dungeon:animated_block", "Animated Block", 
	"mobs_dungeon_animated_block_egg.png", 1
)

-------------------------------------------------------------------------------
--Ooze
local function oozeTextures(num,ra)
	if num==0 then
		return {"mobs_dungeon_ooze0.png"}
	else
		return {"mobs_dungeon_ooze"..num.."_"..ra..".png"}
	end
end

mobs:register_mob(
	"mobs_dungeon:ooze", 
	{
		type="monster",
		passive=false,
		attack_type="dogfight",
		reach=2,
		damage=2,
		hp_min=30,
		hp_max=40,
		armor=80,
		collisionbox={-0.2, -0.01, -0.2, 0.2, 0.4, 0.2},
		visual_size={x=2, y=2},
		visual="mesh",
		mesh="slime_land.b3d",
		textures={
			{"mobs_dungeon_ooze0.png",}
		},
		makes_footstep_sound=false,
		sounds={
			random="mobs_dungeon_ooze_random",
			attack="mobs_dungeon_ooze_attack",
			death="mobs_dungeon_ooze_death"
		},
		view_range=10,
		walk_velocity=3,
		run_velocity=5,
		drops={},
		water_damage=0,
		lava_damage=1,
		light_damage=0,
		animation={
			idle_start=0,
			idle_end=20,
			move_start=21,
			move_end=41,
			fall_start=42,
			fall_end=62,
			jump_start=63,
			jump_end=83
		},
		do_custom=function(self)
			tmw_slimes.animate(self)
		end,
		on_spawn=function(self) advance(self.object,oozeTextures) end
	}	
)

mobs:spawn(
	{
		name="mobs_dungeon:ooze",
		nodes={"group:cracky"},
		chance=3000,
		active_object_count=2,
	}
)

mobs:register_egg("mobs_dungeon:ooze", "Ooze", "mobs_dungeon_ooze0.png", 1)

